package fr.labri.macchiato.client.share;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.IconButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;

import fr.labri.macchiato.framework.technical.share.ShareFunctionalities;

/**
 * A set of buttons allowing the user to share his project
 * 
 * @author Matthieu FOUCAULT
 * 
 */
public class ShareButtons {

	
	/**
	 * 
	 * @param projectName
	 * @return a button that will post a link to the given project on the user's facebook wall
	 */
	public static Canvas facebookShareButton(final String projectName) {
		
		IconButton shareButton = new IconButton("Partager le projet");
		shareButton.setAutoFit(true);
		shareButton.setIcon("http://profile.ak.fbcdn.net/hprofile-ak-snc4/174834_20531316728_3694083_n.jpg");
		shareButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				ShareFunctionalities.shareProjectOnFacebook(projectName);	
			}
		});
		
		return shareButton;
	}
	
	
	/**
	 * 
	 * @param projectName
	 * @return A Canvas displaying a google +1 button that will share the
	 *         project with the given name
	 */
	public static Canvas googlePlusOneButton(String projectName) {

		String url = ShareFunctionalities.getSharedURL(projectName);
		HTMLFlow plusOneButton = new HTMLFlow(
				"<div class=\"g-plusone\" href=\"" + url + "\" ></div>");
		addGooglePlusOneHandler();
		return plusOneButton;
	}

	/**
	 * Adds handler and styles to the +1 button.
	 */
	private static native void addGooglePlusOneHandler() /*-{
		$wnd.___gcfg = {
			lang : 'fr'
		};

		(function() {
			var po = $doc.createElement('script');
			po.type = 'text/javascript';
			po.async = true;
			po.src = 'https://apis.google.com/js/plusone.js';
			var s = $doc.getElementsByTagName('script')[0];
			s.parentNode.insertBefore(po, s);
		})();
	}-*/;

}
